#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 21
 
int board[MAX][MAX];
int w, h, ans;
int da[5] = {-1, 0, 0, 1};
int db[5] = {0, -1, 1, 0};
 
void dfs (int depth, int a, int b, int dir)
{
    a += da[dir];
    b += db[dir];
    if(a <= 0 || b <= 0 || a > h || b > w || depth > 10) return;
    if(board[a][b] == 1){   
        board[a][b] = 0;
        for(int i = 0; i < 4; i ++)
            if(board[a - da[dir] + da[i]][b - db[dir] + db[i]] != 1)
                dfs(depth + 1, a - da[dir], b - db[dir] , i);
        board[a][b] = 1;
    }
    else if(board[a][b] == 3){
        if(depth < ans){
            ans = depth;
            return;
        }
    }
    else  dfs(depth, a, b, dir);
}
 
int main(){
    int a, b, i, j;
 
    while(scanf("%d %d", &w, &h) && w != 0){
        memset(board, 0, sizeof(board));
        for(i = 1; i <= h; i ++)
            for(j = 1; j <= w; j ++){
                scanf("%d", &board[i][j]);
                if(board[i][j] == 2){
                    a = i; b = j;
                }
            }
 
        for(ans = 9999, i = 0; i < 4; i ++)
            if(board[a + da[i]][b + db[i]] != 1)
                dfs(1, a, b, i);
        if(ans == 9999) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}
